﻿package net.fishbowlmedia.scroller3d.control 
{
	import net.fishbowlmedia.scroller3d.model.MoviePlaneModel;
	
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.cameras.FreeCamera3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.scenes.Scene3D;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	* ...
	* @author Tom Lauck
	*/
	public class MoviePlaneControl extends Sprite
	{		
		private var _sceneWidth:int;
		private var _sceneHeight:int;
		private var _camera:FreeCamera3D;
		private var _currentViewport:Viewport3D;
		private var _scene:Scene3D;	
		
		private var _totalPlanes:int;
		private var _selectedPlaneId:int;
		private var _selectedMoviePlane:MoviePlaneModel;
		private var _offset:int;
		private var _seperation:int;
		
		private var _planeClickId:int;
		
		public var displayObject3D:DisplayObject3D;
		private var _moviePlanes:Array;
		
		public function MoviePlaneControl() 
		{
			var moviePlane:MoviePlaneModel = new MoviePlaneModel;
			moviePlanes = new Array();
		}
		public function createDisplayObject():void
		{
			scene.addChild(new DisplayObject3D() , "center" );			
			displayObject3D = scene.getChildByName("center");
			camera.lookAt(displayObject3D);
		}
		public function createMoviePlane(id:int, targetMovie:MovieClip):MoviePlaneModel
		{
			var moviePlaneModel = new MoviePlaneModel();			
			moviePlaneModel.planeId = id;
			moviePlaneModel.targetMovie = targetMovie;
			moviePlaneModel.planeWidth = targetMovie.width;
			moviePlaneModel.planeHeight = targetMovie.height;
			moviePlaneModel.init();			
			moviePlaneModel.plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onPlaneClick);
			return moviePlaneModel;
		}
		private function onPlaneClick(e:InteractiveScene3DEvent):void
		{
			planeClickId = e.currentTarget.id;
			selectedMoviePlane = moviePlanes[e.currentTarget.id] as MoviePlaneModel;
			dispatchEvent(new Event("PlaneClick", false));
		}
		public function get sceneWidth():int { return _sceneWidth; }
		
		public function set sceneWidth(value:int):void 
		{
			_sceneWidth = value;
		}
		
		public function get sceneHeight():int { return _sceneHeight; }
		
		public function set sceneHeight(value:int):void 
		{
			_sceneHeight = value;
		}
		
		public function get camera():FreeCamera3D { return _camera; }
		
		public function set camera(value:FreeCamera3D):void 
		{
			_camera = value;
		}		
		
		public function get scene():Scene3D { return _scene; }	
		
		public function set scene(value:Scene3D):void 
		{
			_scene = value;
		}
		
		public function get totalPlanes():int { return _totalPlanes; }
		
		public function set totalPlanes(value:int):void 
		{
			_totalPlanes = value;
		}
		
		public function get selectedPlaneId():int { return _selectedPlaneId; }
		
		public function set selectedPlaneId(value:int):void 
		{
			_selectedPlaneId = value;
		}
		
		public function get moviePlanes():Array { return _moviePlanes; }
		
		public function set moviePlanes(value:Array):void 
		{
			_moviePlanes = value;
		}
		
		public function get offset():int { return _offset; }
		
		public function set offset(value:int):void 
		{
			_offset = value;
		}
		
		public function get seperation():int { return _seperation; }
		
		public function set seperation(value:int):void 
		{
			_seperation = value;
		}
		
		public function get planeClickId():int { return _planeClickId; }
		
		public function set planeClickId(value:int):void 
		{
			_planeClickId = value;
		}
		
		public function get selectedMoviePlane():MoviePlaneModel { return _selectedMoviePlane; }
		
		public function set selectedMoviePlane(value:MoviePlaneModel):void 
		{
			_selectedMoviePlane = value;
		}
		
		public function get currentViewport():Viewport3D { return _currentViewport; }
		
		public function set currentViewport(value:Viewport3D):void 
		{
			_currentViewport = value;
		}
		
	}
	
}